// pages/form-page/sign-up/sign-up.js
const { testMobile } = require('../../../utils/util.js');
const { gbook } = require('../../../config/api/form.js');
const { mayjorList, schoolList, rulesList, addressList } = require('../../../config/api/index.js');
var app = getApp();
let self
Page({

  /**
   * 页面的初始数据
   */
  data: {
		placeholder: '请详细描述您存在的疑问',
		commentValue: '',  // 报名疑问
		inputName: '请输入学生姓名',  // 报名姓名
		inputPhone: '请输入联系电话',  // 报名电话
		inputMajor: '',  // 报名专业
		inputSchool: '',  // 报名学校
		major: [{ id: -2, name: '请选择专业', pid: '', level: '' }],  // 专业列表
		school: [{ id: -2, name: '请选择学校', pid: '', level: '' }],  // 学校列表
		other: [{ id: -1, name: '其他', pid: '', level: '' }],  // 其他
		indexMajor: 0,  // 当前专业下标
		indexSchool: 0,  // 当前学校下标
		submitStast: false,  // 提交状态判断
		emptyStast: false,  // 清空状态
		pageType: '',  // 页面属性 
		schoolId:'',  // 学校列表页跳转信息 学校id
		schoolName: '',  // 学校列表页跳转信息 学校name
		rulesName: '',  // 简章跳转信息 简章名称
		pageTitle:'预约报名'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		self = this

		wx.setNavigationBarTitle({
			title: options.title || '预约报名',
		})
		if (options.filters != "rules" && options.filters != "rulesnews") {
			self.setData({
				placeholder: '请详细描述您所需要的帮助'
			})
		}

		self.setData({
			pageTitle: options.title || '预约报名',
			pageType: options.filters || '',
			inputMajor: options.project_id || '',
			inputSchool: options.school_id || options.schoolId || '',
			schoolId: options.schoolId || '',
			schoolName: options.schoolName || '',
			rulesName: options.rules_name || ''
		})
		mayjorList({
			data: { pid: 419, per_page: 20 },
			success(res) {
				let major = self.data.major.concat(res.data.items)
				self.setData({
					major
				})
				if (self.data.pageType == 'schoollist'){  // 获取对应简章专业
					let majorid = major.map(function (major) { return major.id; });
					let inputMajor = self.data.inputMajor;
					let index = majorid.findIndex((value, index, arr) => { return value == inputMajor })
					if (index!= -1){
						self.setData({
							indexMajor: index,  // 报名专业
						})
					} else {
						self.setData({
							indexMajor: 0,
							inputMajor: '',  // 报名专业
						})
					}
				}
			
			}
		})
		self.getSchoolFilter()
	
		
  },
  onShow: function () {
  
  },
	getSchoolFilter(params) {  // 获取全部学校列表
		const { filterPage, school, other } = self.data;
		return schoolList({
			data: {
				...{
					per_page: 100,
					is_get: 1
				},
				...params
			},
			success(res) {
				let totalPages = res.data.total_pages
				let resLength = res.data.total_items
				if (filterPage <= totalPages && resLength > 100) {
					self.setData({
						filterPage: filterPage + 1,
						school: [...school, ...res.data.items]
					})
					setTimeout(function () {
						self.getSchoolFilter({ page: filterPage + 1 })
					}, 0)
				} else {
					self.setData({
						school: [...school, ...res.data.items, ...other]
					})
				}
				if (self.data.pageType == 'rules'){
					let schoolList = self.data.school;

					let schoolid = schoolList.map(function (schoolList) { return schoolList.id; });
					let inputSchool = self.data.inputSchool;

					let index = schoolid.findIndex((value, index, arr) => { return value == inputSchool })
					if (index != -1) {  // 获取对应简章学校
						self.setData({
							indexSchool: index,  // 报名学校
						})
					} else {
						self.setData({
							indexSchool: 0,
							inputSchool: '',  // 报名学校
						})
					}
				}	
			}
		})
	},
	commentBind: function (e){
		this.setData({
			commentValue: e.detail.value
		})
	},
	onSubmit: function () {
		const { inputName, inputPhone, inputMajor, inputSchool, rulesName, commentValue, submitStast, pageType, school, indexSchool, major, indexMajor, pageTitle, schoolName} = self.data
		if ( !submitStast && inputName =='请输入学生姓名'){
			wx.showToast({
				title: '姓名不能为空',
				image: '/assert/imgs/error_tip.png',
			})
			return false;
		}
		if (!testMobile(inputPhone)) {
			wx.showToast({
				title: '手机号码错误',
				image: '/assert/imgs/error_tip.png',
			})
			return false;
		}
		if (!inputMajor || inputMajor== -2) {
			wx.showToast({
				title: '专业不能为空',
				image: '/assert/imgs/error_tip.png',
			})
			return false;
		}
		if (!inputSchool || inputSchool == -2) {
			wx.showToast({
				title: '学校不能为空',
				image: '/assert/imgs/error_tip.png',
			})
			return false;
		}
		if (!submitStast){
			wx.showToast({
				title: '隐私保障为勾选',
				image: '/assert/imgs/error_tip.png',
			})
			return false;
		}
		let name = school[indexSchool]['name'] == '请选择学校' ? schoolName : school[indexSchool]['name'] 
		let formInfo = {
			"project": parseInt(inputMajor) || '',
			"school": parseInt(inputSchool) || '',
			"custom": inputName || '',
			"phone": inputPhone || '',
			"mark": commentValue || '',
			"content": (rulesName ? name+ '-' + rulesName + '-' : name  + '-' + major[indexMajor]['name'] + '-' + (pageType !='faq'? pageType =='schoollist' ? '学校-' : '新闻-' :'')) + pageTitle ,
			subsite_id: 52
		}
		gbook({
			data: formInfo,
			success(res) {
				if (res.errCode == 200){
					let inputMajor = (pageType == "faq" || pageType == "rulesnews" || pageType == "schoollist") ? '' : self.data.inputMajor
					let inputSchool = (pageType == "faq" || pageType == "rulesnews") ? '' : self.data.inputSchool
					let indexMajor = (pageType == "faq" || pageType == "rulesnews" || pageType == 'schoollist') ? 0 : self.data.indexMajor
					let indexSchool = (pageType == "faq" || pageType == "rulesnews") ? 0 : self.data.indexSchool

					self.setData({
						inputName: '请输入学生姓名', 
						inputPhone: '请输入联系电话',
						submitStast: false, 
						emptyStast: false,
						inputMajor,
						inputSchool,
						indexMajor,
						indexSchool,
					})
				
				
					wx.showToast({
						title: '提交成功',
						image: '/assert/imgs/success_tip.png',
					})
				} else {
					self.setData({
						emptyStast: true
					})
					wx.showToast({
						title: '提交失败',
						image: '/assert/imgs/error_tip.png',
					})
				}
				
			},
			fail(res){

			}
		})
	},
	checkboxChange: function (e){
		self.setData({
			submitStast: e.detail.value == "1" ? true : false,
		})
	},
	inputName: function (e) {
		self.setData({
			inputName: e.detail.value
		})
	},
	
	inputNameBlur: function (e){
		self.setData({
			inputName: e.detail.value || '请输入学生姓名',
			submitStast: e.detail.value ? true : false
		})
	},
	inputPhoneBlur: function (e) {
		self.setData({
			inputPhone: e.detail.value || '请输入联系电话',
			submitStast: e.detail.value ? true : false
		})
	},
	inputPhone: function (e) {
		self.setData({
			inputPhone: e.detail.value
		})
	},
	inputMajor: function (e) {
		self.setData({
			inputMajor: e.detail.value
		})
		
	},
	majorChange: function (e) {
		this.setData({
			indexMajor: e.detail.value,
			inputMajor: self.data.major[e.detail.value].id
		})
	},
	schoolChange: function (e) {
		self.setData({
			indexSchool: e.detail.value,
			inputSchool: self.data.school[e.detail.value].id
		})
	},
	inputSchool: function (e) {
		self.setData({
			inputSchool: e.detail.value
		})
	},
	guaranteePage: function () {
		wx.navigateTo({
			url: '/pages/guarantee/guarantee',
		})
	},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
		return {
			title: '在职研究生网' + self.data.pageTitle,
			path: '/pages/form-page/sign-up/sign-up'
		}
  }
})